
$(function(){
	
	/**
	 * 模版1 非通用  
	 */
	function Model(){
		
		var mode=new Object();
		
		mode.head_picurl='';
		//图片广告属性
		 mode.picurl='';
		 mode.picurl_temp='assets/goods/img/iamge_home.jpg';
		 mode.picurl_carousel_temp='assets/goods/img/image_carousel.png';
		 mode.piclink='';
		 //文本导航属性
		 mode.text_nav='文本导航';
		 mode.text_nav_link='';
		 
		 //一大两小商品属性
		 mode.goods_three_1_link='';
		 mode.goods_three_1_id='';
		 mode.goods_three_2_link='';
		 mode.goods_three_2_id='';
		 mode.goods_three_3_link='';
		 mode.goods_three_4_id='';
		 
		 mode.title_1='标题名称';
		 mode.title_1_link='';
		 mode.title_2='标题名称';
		 mode.title_2_link='';
		 
		 
		 
		 mode.classify_name='';
		 mode.classify_id='';
		 
		 //4图片导航属性
		 mode.four_nav_image_1='';
		 mode.four_nav_image_2='';
		 mode.four_nav_image_3='';
		 mode.four_nav_image_4='';
		 
		 mode.four_nav_image_1_temp='assets/goods/img/image_nav.png';
		 mode.four_nav_image_2_temp='assets/goods/img/image_nav.png';
		 mode.four_nav_image_3_temp='assets/goods/img/image_nav.png';
		 mode.four_nav_image_4_temp='assets/goods/img/image_nav.png';
		 mode.four_nav_link_1='';
		 mode.four_nav_link_2='';
		 mode.four_nav_link_3='';
		 mode.four_nav_link_4='';
		 
		 // 商品模版 一大两小
		 mode.goods_three_1_link='';
		 mode.goods_three_2_link='';
		 mode.goods_three_3_link='';
		 goods_three_1_id='';
		 goods_three_2_id='';
		 goods_three_3_id='';
		 
		 
		 //四图轮播
		 mode.four_image_carousel_1='';
		 mode.four_image_carousel_2='';
		 mode.four_image_carousel_3='';
		 mode.four_image_carousel_4='';
		 mode.four_image_carousel_link_1='';
		 mode.four_image_carousel_link_2='';
		 mode.four_image_carousel_link_3='';
		 mode.four_image_carousel_link_4='';
		 
		 return mode;
		
	}
	    	
	    	(function($){  
	            $.fn.serializeJson=function(){  
	                var serializeObj={};  
	                var array=this.serializeArray();  
	                var str=this.serialize();  
	                $(array).each(function(){  
	                    if(serializeObj[this.name]){  
	                        if($.isArray(serializeObj[this.name])){  
	                            serializeObj[this.name].push(this.value);  
	                        }else{  
	                            serializeObj[this.name]=[serializeObj[this.name],this.value];  
	                        }  
	                    }else{  
	                        serializeObj[this.name]=this.value;   
	                    }  
	                });  
	                return serializeObj;  
	            };  
	        })(jQuery); 
	    			 
	    	//-------------加载数据 
	    	var mode_id=$("#this_model_id").val()==null?1:$("#this_model_id").val();
			$.ajax({
				type:'post',//可选get
				url:'index.php?r=homemodel/GetUserModelData',
			  data:{'model_id':mode_id},
				dataType:'json',
				success:function(data){
					var model_ =new Model();
					  for(var i=0;i<data.length;i++){
						
						  switch (data[i].model) {
						case 'image':
							model_.picurl=data[i].image;
							model_.piclink=data[i].link;
							break;
							
						case 'text_nav':
							model_.text_nav=data[i].title;
							model_.text_nav_link=data[i].link;
							break;
							
						case 'goods_three':
							model_.goods_three_1_link=data[i].goods_three_1_link;
							model_.goods_three_2_link=data[i].goods_three_2_link;
							model_.goods_three_3_link=data[i].goods_three_3_link;
							model_.goods_three_1_id=data[i].goods_three_1_id;
							model_.goods_three_2_id=data[i].goods_three_2_id;
							model_.goods_three_3_id=data[i].goods_three_3_id;
							break;
							
						case 'title':
							if(model_.title_1=='标题名称'){
								model_.title_1=data[i].title;
								model_.title_1_link=data[i].link;
							}else{
								model_.title_2=data[i].title;
								model_.title_2_link=data[i].link;
								
							}
							
							break;
						case 'classify':
							model_.classify_name=data[i].classify_name;
							model_.classify_id=data[i].classify_id;
							break;
							
							
						case 'four_nav':
							model_.four_nav_image_1=data[i].four_nav_image_1;
							model_.four_nav_image_2=data[i].four_nav_image_2;
							model_.four_nav_image_3=data[i].four_nav_image_3;
							model_.four_nav_image_4=data[i].four_nav_image_4;
							model_.four_nav_link_1=data[i].four_nav_link_1;
							model_.four_nav_link_2=data[i].four_nav_link_2;
							model_.four_nav_link_3=data[i].four_nav_link_3;
							model_.four_nav_link_4=data[i].four_nav_link_4;
							break;
							
						case 'head':
							model_.head_picurl=data[i].image;
							break;
							
						case 'image_carousel':
							model_.four_image_carousel_1=data[i].four_nav_image_1;
							
							model_.four_image_carousel_2=data[i].four_nav_image_2;
							model_.four_image_carousel_3=data[i].four_nav_image_3;
							model_.four_image_carousel_4=data[i].four_nav_image_4;
							model_.four_image_carousel_link_1=data[i].four_nav_link_1;
							model_.four_image_carousel_link_2=data[i].four_nav_link_2;
							model_.four_image_carousel_link_3=data[i].four_nav_link_3;
							model_.four_image_carousel_link_4=data[i].four_nav_link_4;
							break;
							
							
						

						default:
							break;
						}
						  
						  
					  }
					  initModel(model_);
					
	},
	error:function(){
alert("非常抱歉!服务器加载数据异常.")
	}
	})
	    	
function initModel(model){	
				var url_head=$("#url_head_lijigou").val();

	var myViewModel =
    {
				head_picurl:ko.observable(model.head_picurl),
				 picurl:ko.observable(model.picurl),
				 picurl_temp:ko.observable(model.picurl==''?model.picurl_temp:url_head+model.picurl),//用户显示页面图片
				piclink:ko.observable(model.piclink),
				 //文本导航属性
				 text_nav:ko.observable(model.text_nav),
				text_nav_link:ko.observable(model.text_nav_link),
				 
				 //一大两小商品属性
				goods_three_1_link:ko.observable(model.goods_three_1_link),
				goods_three_1_id:ko.observable(model.goods_three_1_id),
				goods_three_2_link:ko.observable(model.goods_three_2_link),
				 goods_three_2_id:ko.observable(model.goods_three_2_id),
				 goods_three_3_link:ko.observable(model.goods_three_3_link),
				goods_three_3_id:ko.observable(model.goods_three_3_id),
				 
				 title_1:ko.observable(model.title_1),
				title_1_link:ko.observable(model.title_1_link),
				title_2:ko.observable(model.title_2),
				 title_2_link:ko.observable(model.title_2_link),
				 
				classify_name:ko.observable(model.classify_name),
				classify_id:ko.observable(model.classify_id),
				 
				 //4图片导航属性
				 four_nav_image_1:ko.observable(model.four_nav_image_1),
				 four_nav_image_1_temp:ko.observable(model.four_nav_image_1==''?model.four_nav_image_1_temp:url_head+model.four_nav_image_1),//用户显示页面图片
				four_nav_image_2:ko.observable(model.four_nav_image_2),
				four_nav_image_2_temp:ko.observable(model.four_nav_image_2==''?model.four_nav_image_2_temp:url_head+model.four_nav_image_2),//用户显示页面图片
				four_nav_image_3:ko.observable(model.four_nav_image_3),
				four_nav_image_3_temp:ko.observable(model.four_nav_image_3==''?model.four_nav_image_3_temp:url_head+model.four_nav_image_3),//用户显示页面图片
				four_nav_image_4:ko.observable(model.four_nav_image_4),
				four_nav_image_4_temp:ko.observable(model.four_nav_image_4==''?model.four_nav_image_4_temp:url_head+model.four_nav_image_4),//用户显示页面图片
				four_nav_link_1:ko.observable(model.four_nav_link_1),
				four_nav_link_2:ko.observable(model.four_nav_link_2),
				four_nav_link_3:ko.observable(model.four_nav_link_3),
			   four_nav_link_4:ko.observable(model.four_nav_link_4),
			   
			   
			   
	 four_image_carousel_1:ko.observable(model.four_image_carousel_1),
	 four_image_carousel_2:ko.observable(model.four_image_carousel_2),
	 four_image_carousel_3:ko.observable(model.four_image_carousel_3),
	 four_image_carousel_4:ko.observable(model.four_image_carousel_4),
	
	 four_image_carousel_link_1:ko.observable(model.four_image_carousel_link_1),
	 four_image_carousel_link_2:ko.observable(model.four_image_carousel_link_2),
	 four_image_carousel_link_3:ko.observable(model.four_image_carousel_link_3),
	 four_image_carousel_link_4:ko.observable(model.four_image_carousel_link_4),
	
	
    }
    ko.applyBindings(myViewModel);
			}
	
function EditModel(){
		
		var edit=new Object();
		edit.text_nav='<form class="form-horizontal" novalidate=""> <div class="control-group"> </div> </div> <div class="separate-line-wrap"> <hr> <div class="separate-line"> <p class="ta-c">文本/链接设置</p> <p class="ta-c">v</p> </div> </div> <div class="control-group"> <ul class="choices"> <li class="choice"><div class="control-group"> <label class="control-label"><em class="required">*</em>导航名称：</label> <div class="controls"> <input type="text" name="title[c14]" value=""  data-bind="value: myValue"> </div></div><div class="control-group"> <label class="control-label"><em class="required">*</em>链接到：</label> <div class="controls"> <input type="text" name="link_url" value="" class="js-fullfill-url"> </div></div><div class="actions"> <span class="action add close-modal" title="添加">+</span><span class="action delete close-modal" title="删除">×</span></div></li></ul> </div> </form>';
        edit.image_ad='<form class="form-horizontal" novalidate=""> <div class="control-group"> <div class="controls">  </div> </div> <div class="separate-line-wrap"> <hr> <div class="separate-line"> <p class="ta-c">图片/链接设置</p> <p class="ta-c">v</p> </div> </div> <div class="control-group"> <ul class="choices"> <li class="choice"><div class="control-group"> <label class="control-label"><em class="required">*</em>选择图片：</label> <div class="controls" name="image_url"> <a class="control-action js-trigger-image" href="javascript: void(0);">选择图片</a> </div></div><div class="control-group"> <label class="control-label">图片标题：</label> <div class="controls"> <input class="input-xxlarge" type="text" name="title[c34]" value=""> </div></div><div class="control-group"> <label class="control-label">链接到：</label> <div class="controls"> <div class="dropdown hover"> <a class="js-dropdown-toggle dropdown-toggle control-action" href="javascript:void(0);">设置链接到的页面地址 <i class="caret"></i></a> <ul class="dropdown-menu"> <li> <a class="js-modal-magazine" data-type="feature" href="javascript:void(0);">微杂志及分类</a> </li> <li> <a class="js-modal-goods" data-type="goods" href="javascript:void(0);">商品及分类</a> </li> <li> <a class="js-modal-survey" data-type="survey" href="javascript:void(0);">投票调查</a> </li> <li> <a class="js-modal-homepage" data-type="homepage" href="javascript:void(0);">店铺主页</a> </li> <li> <a class="js-modal-usercenter" data-type="usercenter" href="javascript:void(0);">会员主页</a> </li> <li> <a class="js-modal-links" data-type="links" href="javascript:void(0);">自定义外链</a> </li></ul> </div> </div></div><div class="actions"> <span class="action add close-modal" title="添加">+</span><span class="action delete close-modal" title="删除">×</span></div></li></ul> </div> <div class="control-group options"> <div class="controls" name="sub_entry"> <a href="javascript:void(0);" class="js-add-option">+添加一个图片广告</a> </div> </div></form>';		
		return edit;
	};
	
	var edit=new EditModel();
	
	$("#homeset_edit").click(function(){
		var top=parseInt($(this).offset().top)-145;
		$(".app-sidebar").css("margin-top",top+"px")
		$(".app-setting-header").html(edit.text_nav);
		$(".app-sidebar").show();
	});
	
	$("#save_ljigou_homeset").click(function(){
		var mode_id=$("#this_model_id").val();
		
		var jsonstr='[';
		$("form").each(function(){
			 var a=$(this).serializeJson();
			 jsonstr+=JSON.stringify(a)+",";
		})
		jsonstr=jsonstr.substring(0,jsonstr.length-1);
		jsonstr+="]";
	
		
		$.ajax({
			type:'post',//可选get
			url:'index.php?r=homemodel/saveModeData',
			data:{"data":encodeURIComponent(jsonstr),"model_id":mode_id}, //转码 否则无法保存复杂的url
			dataType:'text',
			success:function(msg){
				$('#save_home_title').css("margin-top",$(document).scrollTop()+"px");

				if(msg==1){
					$('#save_home_title_c').text('保存成功');
				}else{
					
					$('#save_home_title_c').text('抱歉 ,保存出错了 !');
				}
				
				$('#save_home_title').modal('show');

},
error:function(){

}
})
		
	});
	
$("#userclassify").click(function(){
	
	var ob=$("#classify_object").val();
	
	 var classify_id=$("#classify_id").val();
	 if(classify_id=="")return;
  if(ob=='cname_ljg'){
		 var classify_name=$("#classify_name").val();
  $("#cname_ljg").val(classify_name);
  $("#cid_ljg").val(classify_id);
	$('#classifyselect').modal('hide');
	$("#classify_id").val("");
	$("#classify_name").val("");

  return;
  }
	$("#"+ob).val($("#url_temp").val()+"/index.php?r=wap/Showgroup&pid="+$("#pid_temp").val()+"&classifyid="+classify_id)
	$('#classifyselect').modal('hide');
	$("#classify_id").val("");
	$("#classify_name").val("");

	
})




$("#usegoods").click(function(){
	var ob=$("#classify_object").val();
	 var goodsid=$("#goods_id").val();
	if(ob=='goods1'||ob=='goods2'||ob=='goods3'){
		
		$("#"+ob+"_id").val(goodsid);
	}
	
	
	 $("#"+ob).val($("#url_temp").val()+"/index.php?r=wap/Detail&product="+$("#goods_name").val());
		$('#goodselect').modal('hide');
		$("#goods_id").val("");
		$("#goods_name").val("");
	
	
})
	

$('#classifypage .yiiPager a').on('click',function(){

    $.ajax({
    	type: "post",
    	dataType:"json",
        url:$(this).attr('href')+"&type=1",  //1:请求商品分类
        success:function(html){
           
        var name="";
        var id="";
        var time='';
        var d= new Date();
        $("#classify_tbd").html("");
        for(var i=0;i<html.length;i++){
                 name=html[i].name;
                 id=html[i].id;
                 d.setTime(html[i].createtime+'000');
                 time=d.format('yyyy-MM-dd ');
     	  var tr= '<tr> <td class="title"> <div class="td-cont"> <a target="_blank" class="new_window" href="" >'+name+'</a> </div></td> <td class="time"> <div class="td-cont"> <span>'+time+'</span> </div></td><td class="opts"><div class="td-cont"> <button class="btn js-choose" href="javascript:void(0);"   onclick=selectclassifyid("'+id+'","'+name+'") >选取</button> </div></td></tr>';
     	  $("#classify_tbd").append(tr);
            }
            
        }
    });
    return false;
})



$('#goodspage .yiiPager a').on('click',function(){

    $.ajax({
    	type: "post",
    	dataType:"json",
        url:$(this).attr('href')+"&type=2",  //1:请求商品分类
        success:function(html){
           
        var name="";
        var id="";
        var time='';
        var d= new Date();
        $("#goods_tbd").html("");
        for(var i=0;i<html.length;i++){
     	   d.setTime(html[i].createtime+'000');
           time=d.format('yyyy-MM-dd ');
                 name=html[i].small_commodity_name;
                 id=html[i].id;
     	  var tr= '<tr> <td class="title"> <div class="td-cont"> <a target="_blank" class="new_window" href="" >'+name+'</a> </div></td> <td class="time"> <div class="td-cont"> <span>'+time+'</span><br/><span></span> </div></td><td class="opts"><div class="td-cont"> <button class="btn js-choose" href="javascript:void(0);"   onclick=selectgoodsid("'+id+'","'+html[i].commodity_no+'") >选取</button> </div></td></tr>';
     	  $("#goods_tbd").append(tr);
            }
            
        }
    });
    return false;
})

});

function selectClassify(va,ob){
	
	//$('#classifyselect').modal('show');
	$('#classifyselect').css("margin-top",$(document).scrollTop()+"px");
	$('#classifyselect').modal('show');
	$("#classify_object").val(va)
	//alert(va)
}

function selectgoods(va,ob){
	
	//$('#classifyselect').modal('show');
	$('#goodselect').css("margin-top",$(document).scrollTop()+"px");
	$('#goodselect').modal('show');
	$("#classify_object").val(va);
	//alert(va)
	
}
function selectclassifyid(id,name){
	$("#userclassify").show('slow');
	$("#classify_name").val(name);
	$("#classify_id").val(id);

	
}

function selectgoodsid(id,name){
	$("#usegoods").show('slow');
	$("#goods_name").val(name);
	$("#goods_id").val(id);
}



function selectHome(ob){
	
	$("#"+ob).val($("#url_temp").val()+'/index.php?r=wap/Homepage&pid='+$("#pid_temp").val());
	
}
